﻿using System;
using System.Web;
using System.Web.Security;
using System.Web.Configuration;
using FPC.Offer.Model;



namespace FPC.Offer.Common
{
    /// <summary>
    /// 用户登录操作类
    /// </summary>
    public class SysAccountLogin
    {
        #region 用户信息注册

        /// <summary>
        /// 用户登录身份表单验证(注册票据)
        /// </summary>
        /// <param name="ud">登陆者信息</param>
        public static void Signin(Model.Police_FuzzTable sd)
        {
            //把用户登陆数据进行￥串联起来，以备注册使用 依次顺序是【账号和角色Id，】
            string struser = sd.LoginName + "$" + sd.RoleId;
            //获取当前请求上下文
            HttpContext context = HttpContext.Current;

            //从表单验证中获取票的超时时间m
            AuthenticationSection config = (AuthenticationSection)
                context.GetSection("system.web/authentication");
            int timeout = (int)config.Forms.Timeout.TotalMinutes;

            //注册登录票据
            System.Web.Security.FormsAuthenticationTicket tk = new FormsAuthenticationTicket(
                    1,
                    sd.AlarmId.ToString(),//此处注册登陆者主键ID
                    DateTime.Now,
                    DateTime.Now.AddMinutes(timeout),
                    false,
                    struser,//此处注册登陆者其它信息
                    FormsAuthentication.FormsCookiePath
                    );

            //得到加密后的身份验证票字串 
            string key = System.Web.Security.FormsAuthentication.Encrypt(tk);

            HttpCookie ck = new HttpCookie(System.Web.Security.FormsAuthentication.FormsCookieName, key);
            // 这句话在部署网站后有用，此为关系到同一个域名下面的多个站点是否能共享Cookie
            //ck.Domain = System.Web.Security.FormsAuthentication.CookieDomain;  
            HttpContext.Current.Response.Cookies.Add(ck);

        }

        #endregion

        #region 用户信息获取
        /// <summary>
        /// 获取登陆用户ID
        /// <para>如果：HttpContext.Current is null 返回 0</para>
        /// <para>如果：未登陆为0</para>
        /// </summary>
        public static int Get_AccountID
        {
            get
            {
                if (HttpContext.Current == null)
                    return 0;

                HttpContext context = HttpContext.Current;
                return context.User.Identity.IsAuthenticated ?
                    Convert.ToInt32(HttpContext.Current.User.Identity.Name) : 0;
            }
        }

        /// <summary>
        /// 获取登录用户的登陆账户
        /// <para>如果：HttpContext.Current is null 返回 空;</para>
        /// </summary>
        public static string Get_Account
        {
            get
            {
                if (HttpContext.Current == null)
                    return "";

                HttpContext context = HttpContext.Current;
                string loginName = string.Empty;
                if (context.User.Identity.IsAuthenticated)
                {
                    FormsIdentity fi = (FormsIdentity)context.User.Identity;
                    FormsAuthenticationTicket Ticket = fi.Ticket; //取得身份验证票

                    //因为在注册票据的时候加入了$以区分用户的信息依次顺序是【账号，角色ID】，拆分$符号
                    string[] str = Ticket.UserData.Split('$');
                    loginName = str[0];
                }
                return loginName;
            }
        }
        /// <summary>
        /// 获取登陆用户的用户角色（类型）
        /// <para>如果：HttpContext.Current is null 返回-1;</para>
        /// </summary>
        public static int Get_RoleID
        {
            get
            {
                if (HttpContext.Current == null)
                    return 0;

                HttpContext context = HttpContext.Current;
                int roleID = 0;
                if (context.User.Identity.IsAuthenticated)
                {
                    FormsIdentity fi = (FormsIdentity)context.User.Identity;
                    FormsAuthenticationTicket Ticket = fi.Ticket; //取得身份验证票

                    //因为在注册票据的时候加入了$以区分用户的信息依次顺序是【账号，角色ID】，拆分$符号
                    string[] str = Ticket.UserData.Split('$');
                    roleID = Convert.ToInt32(str[1]);
                }
                return roleID;
            }
        }
        #endregion
    }
}
